﻿using NUnit.Framework;
using TeamsPortal.DataAccess;
using TeamsPortal.Tests.Controllers;
using System.Data.SqlClient;
using System.Configuration;

namespace TeamsPortal.Tests.DataAccess
{
    [TestFixture]
    public class MessagesDataAccessTests : BaseRollbackTest
    {
        [Test]
        public void GetById()
        {
            User user = DATestsHelper.CreateUser("username", UserRole.Member);
            Forum forum = DATestsHelper.CreateForum(user);
            Topic topic = DATestsHelper.CreateTopic(forum, user);
            var message = new Message() {
                //Active = false, 
                Body = "testBody", 
                //Date = DateTime.Now, 
                //FlagCount = 1, 
                User = user,
                Topic = topic
            };
            var da = new MessagesDataAccess();
            da.Add(message, DATestsHelper.UnitTestUserIP);

            Message loaded = da.Get(message.Id, topic.Id);
            Assert.IsNotNull(loaded);
            Assert.AreEqual(message.Id, loaded.Id);
            Assert.IsNotNull(loaded.Topic);
            Assert.AreEqual(message.Topic.Id, loaded.Topic.Id);
            //Assert.AreEqual(message.Active, loaded.Active);
            Assert.IsTrue(loaded.Active);
            Assert.AreEqual(message.Body, loaded.Body);
            //Assert.AreEqual(message.Date, loaded.Date);
            //Assert.AreEqual(message.FlagCount, loaded.FlagCount);
            Assert.AreEqual(0, loaded.FlagCount);
            Assert.IsNotNull(loaded.User);
            Assert.AreEqual(message.User.Id, loaded.User.Id);
            //Assert.IsFalse(loaded.IsRead);
        }

        [Test]
        public void InsertedMessageMustBeRead()
        {
            User user = DATestsHelper.CreateUser("username", UserRole.Member);
            Forum forum = DATestsHelper.CreateForum(user);
            Topic topic = DATestsHelper.CreateTopic(forum, user);
            var message = new Message()
            {
                Body = "testBody",
                User = user,
                Topic = topic
            };
            var da = new MessagesDataAccess();
            da.Add(message, DATestsHelper.UnitTestUserIP);

            bool marked = isMessageMarkedAsRead(message.Id, topic.Id, user.Id);
            Assert.IsTrue(marked);
        }

        private bool isMessageMarkedAsRead(int messageId, int topicId, int userId)
        {
            var result = false;
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Forums"].ConnectionString))
            {
                var sql = string.Format("select TopicId, MessageId, UserId from dbo.ReadMessages where TopicId={0} and MessageId={1} and UserId={2}", topicId, messageId, userId);
                var cmd = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    result = reader["UserId"] != null;
                }
            }

            return result;
        }
    }
}
